<?php
/**
 * ORA 扩展类库 视频信息采集类
 * 目前支持 youku , tudou
 * @author lan7
 * @copyright lan7.net
 */
class vod_lib {
	static $rule = array(
	'youku'=>array(
	'id'	=>	'/id\_(\w*?)\..*?/i',
	'swf'	=>	'http://player.youku.com/player.php/sid/##ID##/v.swf',
	'pic'	=>	'/\\+0800|(http:\/\/[^\|]*?\/[0-9a-zA-Z\-]*?)\|/i',
	),
	'tudou'=>array(
	'id'	=>	'/\/view\/(\w*?)\//i',
	'swf'	=> 'http://www.tudou.com/v/##ID##/v.swf',
	'pic'	=>	'/\,bigItemUrl\s+=\s+\"(http:\/\/.*?\.jpg)\"/i',
	),
	);
	/**
	 * 获取视频预览图及swf地址
	 *
	 * @param unknown_type $url
	 * @return unknown
	 */
	public static function getVodInf($url){
		$res = array('id'=>0,'url'=>$url,'swf'=>'','pic'=>'','type'=>'');
		if(self::typeof($url)){
			$res['type'] = self::typeof($url);
			self::getVodId($res);
			self::getVodPic($res);
		}else{
			return false;
		}
		return $res;
	}

	public static function getVodId(&$res){
		$tmp = self::$rule;
		preg_match_all($tmp[$res['type']]['id'],$res['url'],$m);
		$res['id'] = $m[1][0];
		$res['swf'] = str_replace("##ID##",$res['id'],$tmp[$res['type']]['swf']);

	}
	public static function getVodPic(&$res){
		$tmp = self::$rule;
		$c = @ file_get_contents($res['url']);
		@preg_match_all($tmp[$res['type']]['pic'],$c,$m);
		if($m[1]){
			if(isset($m[1][2])){
				$res['pic'] = $m[1][2];
			}else{
				$res['pic'] = $m[1][0];
			}
		}
		$fname = "";
		if(!preg_match('/http:.*?\.(jpg|gif|png)/i',$res['pic'])){
			$fname = "vod/".time().rand_string(8,1).".jpg";
		}else{
			$fname = "vod/".time().rand_string(8,1).basename($res['pic']);
		}
		$hdl = fopen($res['pic'], "rb");
		if($hdl){
			$contents = "";
			while (!feof($hdl)) {
				$contents .= fread($hdl, 8192);
			}
			io::write(UPLOAD_PATH.$fname,$contents);
			$res['pic'] = $fname;
		}
	}
	/**
	 * 获取视频网站类型
	 *
	 * @param unknown_type $url
	 * @return unknown
	 */
	public static function typeof($url){
		$keys = array_keys(self::$rule);
		foreach ($keys as $k=>$v){
			if(preg_match('/'.$v.'/i',$url)){
				return $v;
			}
		}
		return false;
	}

}
?>